/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "SettingsDialog.h"
#include "Logger.h"

SettingsDialog::SettingsDialog(QDialog *parent)
    : QDialog(parent)
{
	ui.setupUi(this);

	connect(ui.settingOKButton, SIGNAL(clicked()), this, SLOT(OKButtonClicked()));

	mProgramFontDialog.setCurrentFont(OPT.programFont);
	QObject::connect(ui.setProgramFontButton, SIGNAL(clicked()), this, SLOT(showProgramFontDialog()));

	mBrowserFontDialog.setCurrentFont(OPT.wordBrowserFont);
	QObject::connect(ui.setBrowserFontButton, SIGNAL(clicked()), this, SLOT(showBrowserFontDialog()));
}

SettingsDialog::~SettingsDialog()
{
}

int SettingsDialog::exec()
{
	ui.minTestWordCountSpinBox->setValue(OPT.testWordCountMin);
	ui.firstGroupWaitSecMaxEdit->setValue(OPT.firstGroupWaitSecMax);
	ui.secondGroupWaitSecMinEdit->setValue(OPT.secondGroupWaitSecMin);

	mProgramFontDialog.setCurrentFont(OPT.programFont);
	mBrowserFontDialog.setCurrentFont(OPT.wordBrowserFont);
	QString testString;
	testString = QString("Current font: %1").arg(OPT.programFont.family());
	testString.append(QString(" %1.").arg(OPT.programFont.pointSize()));
	ui.programFontTestLabel->setText(testString);
	ui.programFontTestLabel->setFont(OPT.programFont);
	testString = QString("Current font: %1").arg(OPT.wordBrowserFont.family());
	testString.append(QString(" %1.").arg(OPT.wordBrowserFont.pointSize()));
	ui.wordBrowserFontTestLabel->setText(testString);
	ui.wordBrowserFontTestLabel->setFont(OPT.wordBrowserFont);
	ui.autoSpeakCheckBox->setChecked(OPT.autoSpeak);

	ui.autoStartTrainingBox->setChecked(OPT.autoStartTrainer);
	//ui.trainEpochMSSpinBox->setMaximum(99999999);
	ui.trainEpochMSSpinBox->setValue(OPT.trainEpochMS);
	ui.checkFinishedEpochCountSpinBox->setValue(OPT.checkFinishedEpochCount);
	ui.finishedMSEOfMSEsSpinBox->setValue(OPT.finishedMSEOfMSEs);
	ui.trainEpochMaxSpinBox->setValue(OPT.trainEpochCountMax);

	return QDialog::exec();
}

void SettingsDialog::OKButtonClicked()
{
	OPT.testWordCountMin = ui.minTestWordCountSpinBox->value();
	OPT.firstGroupWaitSecMax = ui.firstGroupWaitSecMaxEdit->value();
	OPT.secondGroupWaitSecMin = ui.secondGroupWaitSecMinEdit->value();
	OPT.autoSpeak = ui.autoSpeakCheckBox->isChecked();

	OPT.autoStartTrainer = ui.autoStartTrainingBox->isChecked();
	OPT.trainEpochMS = ui.trainEpochMSSpinBox->value();
	OPT.checkFinishedEpochCount = ui.checkFinishedEpochCountSpinBox->value();
	OPT.finishedMSEOfMSEs = ui.finishedMSEOfMSEsSpinBox->value();
	OPT.trainEpochCountMax = ui.trainEpochMaxSpinBox->value();

	this->accept();
}

void SettingsDialog::showProgramFontDialog()
{
	mProgramFontDialog.exec();
	OPT.programFont = mProgramFontDialog.currentFont();
	this->setFont(OPT.programFont);
	QString testString;
	testString = QString("Current font: %1").arg(OPT.programFont.family());
	testString.append(QString(" %1.").arg(OPT.programFont.pointSize()));
	ui.programFontTestLabel->setText(testString);
	ui.programFontTestLabel->setFont(OPT.programFont);
}

void SettingsDialog::showBrowserFontDialog()
{
	mBrowserFontDialog.exec();
	OPT.wordBrowserFont = mBrowserFontDialog.currentFont();
	QString testString;
	testString = QString("Current font: %1").arg(OPT.wordBrowserFont.family());
	testString.append(QString(" %1.").arg(OPT.wordBrowserFont.pointSize()));
	ui.wordBrowserFontTestLabel->setText(testString);
	ui.wordBrowserFontTestLabel->setFont(OPT.wordBrowserFont);
}

